import requests
import json
if __name__ == "__main__":
    #1.指定Url
    post_url = 'https://fanyi.baidu.com/sug'
    #2.进行UA伪装
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36 Edg/122.0.0.0'
    }

    #3.post请求参数处理(同get请求一致)
    word = input('enter a word')
    data = {
        'kw': word
    }
    #4.进行请求发送
    response = requests.post(url=post_url, data=data,headers=headers)
    #5.获取响应数据:json()方法返回的是obj(如果服务器确认响应数据是json类型的，才能)
    dic_obj = response.json()

    #6.进行持久化存储
    fileName = word+'.json'
    fp = open(fileName,'w',encoding='utf-8')
    json.dump(obj=dic_obj,fp=fp,ensure_ascii=False)
    print('over')
